<?php
require_once('internal/IModule.interface.php');

class Stats implements IModule {
	public $sections;
	function __construct($engine=NULL) {
		global $lang;
		$this->engine = $engine;
		$this->engine->head->add('script', array('type'=>'text/javascript','src'=>'internal/Plot.js'));
		$this->engine->head->add('script', array('type'=>'text/javascript','src'=>'internal/excanvas.js'), '', true);
		$this->loadSettings();
		$this->sections = array(
			$lang['admin']['stats']['by_months'] => '?action=stats&show=months',
			$lang['admin']['stats']['by_days'] => '?action=stats&show=days',
			$lang['admin']['stats']['browsers'] => '?action=stats&show=browsers',
			$lang['admin']['stats']['platforms'] => '?action=stats&show=platforms'
		);
	}
	function execute() {
	global $lang;
		switch ($_GET['show']) {
			case 'browsers':
				$this->drawGraph($lang['admin']['stats']['browsers_used'].'.', StatsBrowsing::browsers(), NULL, $this->settings['theme'], 'pie');
				break;
			case 'platforms':
				$this->drawGraph($lang['admin']['stats']['platforms_used'].'.', StatsBrowsing::platforms(), NULL, $this->settings['theme'], 'pie');
				break;
			case 'months':
				$date = ($_GET['date']) ? $_GET['date'] : date('Y');
				$this->drawGraph($lang['admin']['stats']['entries_months'].' <b>'.$date.'</b>.', StatsBrowsing::months($date), array($lang['admin']['stats']['unique_users'], $lang['admin']['stats']['all_users']), $this->settings['theme']);
				break;
			case 'days':
			default:
				$date = ($_GET['date']) ? $_GET['date'] : date('Y-m');
				$dt = explode('-', $date);
				$this->drawGraph($lang['admin']['stats']['entries_days'].' <b>'.$lang['months'][$dt[1]].' '.$dt[0].'</b>.', StatsBrowsing::days($date), array($lang['admin']['stats']['unique_users'], $lang['admin']['stats']['all_users']), $this->settings['theme']);
				break;
		}
	}
	private function drawGraph($title, $data, $names, $theme = 'maetheme', $type='bars_double') {
		
		?>
		<script type="text/javascript">
			// <![CDATA[
			window.addEvent('domready', function(){
				var p = new Plot($('plot'), {
					data: <?php echo json_encode($data); ?>,
					type: '<?php echo $type; ?>',
					roundTo:0,
					legend: <?php echo ($type == 'pie') ? 'false':'true'; ?>,
					names: <?php echo json_encode($names); ?>,
					style: <?php echo json_encode($this->settings['theme']); ?>,
				});
			});
			// ]]>
		</script>
		<p><?php echo $title; ?></p>
		<canvas id="plot" width="640" height="480"></canvas>
		<?php
	}
	/*
	 function loadSettings()
	 Loads settings from the XML file
	*/
	protected function loadSettings() {
		$xml = simplexml_load_file('modules/admin/stats/settings.xml');
		$xml = $xml->theme;
		$this->settings['theme'] = array(
			'header' => (string)$xml->header,
			'border' => (string)$xml->border,
			'guides' => (string)$xml->guides,
			'captions' => (string)$xml->captions,
			'captionsPie' => (string)$xml->captionsPie,
			'legend' => (string)$xml->legend,
			'background' => (string)$xml->background,
			'titles' => (string)$xml->titles,
			'dataBorder' => (string)$xml->dataBorder,
			'separator' => (string)$xml->separator,
			'shadowColor' => (string)$xml->shadowColor,
			'lineWidth' => (int)$xml->lineWidth,
			'guideWidth' => (int)$xml->guideWidth,
			'borderWidth' => (int)$xml->borderWidth,
			'shadowOffset' => (int)$xml->shadowOffset,
			'stroke' => (int)$xml->stroke,
			'shadow' => (int)$xml->shadow,
			'autoColor' => (bool)$xml->autoColor
		);
		foreach ($xml->data as $val) $this->settings['theme']['data'][] = (string)$val;
	}
	/*
	function configure()
	Configures the Statistics module
	*/
	public function configure() {
		if ($_POST['text']) $this->save();
		?>
		<script type="text/javascript">
		 function doSave() {
		  $$('form')[0].submit();
		 }
		</script>
		<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
			<textarea name="text" rows="20" cols="70"><?php for ($i=0; $i<count($row); $i++) echo $row[$i]; ?></textarea>
		</form>
		<?php
	}
	/* 
	function save()
	Saves the changes
	*/
	private function save() {
		print '<div id="message">Saved!</div>';
	}
}
?>
